package com.gome.boot.config.elasticsearch.cluster.selector;

import com.gome.boot.config.elasticsearch.cluster.ClusterClient;

import java.util.List;

/**
 * ES多集群HASH规则选择器.
 *
 * @author baoxiufeng
 */
public class SelectClusterByHash extends AbstractSelectCluster {

    @Override
    public ClusterClient select(List<ClusterClient> clients, Object obj) {
        ClusterClient client = select(clients);
        if (client == null) {
            int value = obj.hashCode();
            if (value < 0) {
                value = Math.abs(value);
            }
            client = clients.get(value % clients.size());
        }
        return client;
    }
}
